'use strict';

var config = require('../config/index.js');
var assert = require('assert');
var request = require('request');
var path = require('path');
var __rootUrl = config.rootUrl;
var testUtils = require('./test_utils.js');

var __mockUid = '__mock_user';
var token;
var starid;
var partid;
var ufaceid;

function waitToken(done) {
  if (!!token)done();
  else setTimeout(function () {
    waitToken(done);
  }, 500);
}

function waitStarid(done) {
  if (!!starid)done();
  else setTimeout(function () {
    waitStarid(done);
  }, 500);
}

function waitPartid(done) {
  if (!!partid)done();
  else setTimeout(function () {
    waitPartid(done);
  }, 500);
}
/**
 * 时序
 * => token
 * => 上传用户脸,上传明星脸
 * => 上传明星器官
 * => 编辑明星器官
 * => 获取所有明星列表, 获取某个明星的面部组件, 获取所有面部组件
 * => 对用户的脸进行替换
 */

// done
describe('token', function () {
  this.timeout(2000);
  var theUrl = __rootUrl + '/test/face/token?uid=%uid'.replace('%uid', __mockUid);
  it('获取token', function (done) {
    request(theUrl, function (err, res, data) {
      if (err) console.log(err);
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      if (data.errCode != 0) console.log(data.errMsg);
      assert.equal(0, data.errCode);
      //
      token = data.result.token;
      assert.equal(!!token, true);
      done();
    });
  });
});


describe('接口测试: 添加测试数据1', function () {
  this.timeout(0);
  before(function (done) {
    waitToken(done);
  });

  it('上传用户面部图片', function (done) {
    var theUrl = __rootUrl + '/api/v1/face/user/upload';
    testUtils.post_formMultipart(theUrl, {token: token}, 'face', null, function (err, res, data) {
      if (err) console.log(err);
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      if (data.errCode != 0) console.log(data.errMsg);
      assert.equal(0, data.errCode);
      //
      assert.equal(data.result.uid, __mockUid);
      ufaceid = data.result.ufaceid;
      done();
    });
  });

  it('管理接口:上传用户明星', function (done) {
    var theUrl = __rootUrl + '/manapi/v1/face/star/upload';
    var starName = '__mock_star';
    testUtils.post_formMultipart(theUrl, {
      token: token,
      name: starName
    }, 'face', null, function (err, res, data) {
      if (err) console.log(err);
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      if (data.errCode != 0) console.log(data.errMsg);
      assert.equal(0, data.errCode);
      //
      assert.equal(data.result.name, starName);
      assert.equal(!!data.result.starid, true); // starid >0, =1,2,3..
      starid = data.result.starid;
      done();
    });
  });

});


describe('接口测试: 添加测试数据2', function () {
  this.timeout(0);
  before(function (done) {
    waitStarid(done);
  });

  it('管理接口:添加明星面部组件', function (done) {
    var theUrl = __rootUrl + '/manapi/v1/face/parts/upload';
    var desc = "__mock_brow_add";
    testUtils.post_formMultipart(theUrl, {
      token: token,
      starid: starid,
      browdesc: desc
    }, 'browfile', null, function (err, res, data) {
      if (err) console.log(err);
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      if (data.errCode != 0) console.log(data.errMsg);
      assert.equal(0, data.errCode);
      //
      var brow = data.result.brow;
      assert.equal(!!brow, true);
      assert.notEqual(typeof(brow.otype), 'undefined');
      assert.equal(brow.starid, starid);
      assert.equal(!!brow.partid, true);
      assert.equal(!!brow.url, true);// should exist url
      partid = brow.partid;
      done();
    });
  });
});


describe('接口测试: 使用测试数据1', function () {
  this.timeout(0);
  before(function (done) {
    waitPartid(done);
  });

  it('管理接口:获取所有明星列表', function (done) {
    var theUrl = __rootUrl + '/manapi/v1/face/star/list?token=%token'.replace('%token', token);
    request(theUrl, function (err, res, data) {
      if (err) console.log(err);
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      if (data.errCode != 0) console.log(data.errMsg);
      assert.equal(0, data.errCode);
      //
      assert.equal(!!data.result.length, true); // should > 0
      assert.equal(!!data.result[0].starid, true); // should exist starid
      assert.equal(!!data.result[0].name, true); // should exist name
      done();
    });
  });

  it('获取某个明星的面部组件', function (done) {
    var theUrl = __rootUrl + '/manapi/v1/face/parts?starid=%starid&&token=%token'
        .replace('%token', token)
        .replace('%starid', starid + '');

    request(theUrl, function (err, res, data) {
      if (err) console.log(err);
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      if (data.errCode != 0) console.log(data.errMsg);
      assert.equal(0, data.errCode);
      //
      assert.equal(!!data.result.length, true); // should > 0
      var item0 = data.result[0];
      assert.equal(!!item0.url, true); // should exist url
      assert.equal(!!item0.desc, true); // should exist desc
      assert.equal(item0.starid, starid); // should exist starid
      assert.notEqual(typeof(item0.otype), 'undefined');
      assert.equal(item0.partid, partid); // should exist partid

      done();
    });
  });

  it('获取所有面部组件', function (done) {
    var theUrl = __rootUrl + '/api/v1/face/parts/all?token=%token'.replace('%token', token);
    request(theUrl, function (err, res, data) {
      if (err) console.log(err);
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      if (data.errCode != 0) console.log(data.errMsg);
      assert.equal(0, data.errCode);
      //
      assert.equal(!!data.result.eye, true); // exist brow,eye,nose,mouse
      assert.equal(!!data.result.brow, true);
      assert.equal(!!data.result.nose, true);
      assert.equal(!!data.result.mouse, true);

      var eye0 = data.result.eye[0];
      assert.equal(!!eye0.url, true);
      assert.equal(!!eye0.starid, true);
      assert.notEqual(typeof(eye0.otype), 'undefined');
      assert.equal(!!eye0.partid, true);
      assert.notEqual(typeof(eye0.order), 'undefined');
      assert.notEqual(typeof(eye0.desc), 'undefined');
      done();
    });
  });

  it('对用户的脸进行替换', function (done) {
    assert.equal(!!token, true);
    var theUrl = __rootUrl + '/api/v1/face/render';
    var opt = {
      uri: theUrl,
      method: 'POST',
      json: {
        token: token,
        ufaceid: ufaceid,
        partids: '1'
      }
    };
    request(opt, function (err, res, data) {
      if (err) console.log(err);
      assert.equal(!!err, false);
      if (typeof data === 'string') data = JSON.parse(data);
      if (data.errCode != 0) console.log(data.errMsg);
      assert.equal(0, data.errCode);
      //
      assert.equal(!!data.result.resultUrl, true);
      done();
    });
  });
});
